/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aviso.entity;

import java.io.Serializable;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

/**
 *
 * @author avgusti
 */
@Entity
@NamedQueries ({
@NamedQuery (name="UserName.VerifyEmail", query="select distinct u from UserName u where u.emailHash=:hash")
})
public class UserName implements Serializable {

    public enum Type {

        PERSON(1), AGENCY(2), SITE_STUFF(3);
        private int code;

        private Type(int c) {
            code = c;
        }

        public int getCode() {
            return code;
        }

        public String getLabel() {
            switch (code) {
                case 1:
                    return "person";
                case 2:
                    return "agency";
                case 3:
                    return "stuff";
                default:
                    return "person";
            }
        }
    }
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(unique = true)
    @NotNull
    @Size (min=4, max=8)
    private String name;
    private String contact;
    private String displayname;
// prefs    
    private String locale="ru"; 
    private int historyLength=10;
    private boolean newsSubscription=true;  
//    
//  validate
    private String emailHash;
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar created;
//    
// money
    //@TODO user money
//
// admin
    Boolean active=true;
//    
    @NotNull
    private String passhash;
    @OneToOne
    private ImageFile avatar;
    @NotNull
    @Pattern(regexp="^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$", message="{invalidEmailResourceBundleKey}")
    private String email;
    private String address;
    @NotNull
    private UserName.Type type;
    @OneToMany(orphanRemoval = true, cascade = CascadeType.ALL, fetch= FetchType.EAGER )
    private Map<Long,UserHistoryRecord> history=new HashMap<Long,UserHistoryRecord>();
    @OneToMany(orphanRemoval = true, cascade = CascadeType.ALL, fetch= FetchType.EAGER )
    private Map<Long,UserCartRecord> cart=new HashMap<Long,UserCartRecord>();
    @ManyToOne
    GroupName group;

    public Map<Long, UserCartRecord> getCart() {
        return cart;
    }

    public void setCart(Map<Long, UserCartRecord> cart) {
        this.cart = cart;
    }

    public Map<Long, UserHistoryRecord> getHistory() {
        return history;
    }

    public void setHistory(Map<Long, UserHistoryRecord> history) {
        this.history = history;
    }

    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }
    
    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getDisplayname() {
        return displayname;
    }

    public void setDisplayname(String displayname) {
        this.displayname = displayname;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public GroupName getGroup() {
        return group;
    }

    public void setGroup(GroupName group) {
        this.group = group;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name.toLowerCase();
    }

    public String getPasshash() {
        return passhash;
    }

    public String getPassword() {
        return passhash;
    }

    public void setPasshash(String passhash) {
        this.passhash = passhash;
    }

    public ImageFile getAvatar() {
        return avatar;
    }

    public void setAvatar(ImageFile avatar) {
        this.avatar = avatar;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof UserName)) {
            return false;
        }
        UserName other = (UserName) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return name;
    }

    public String avatarImage() {
        if (avatar != null && id != null) {
            return name + "\\" + avatar.getName();
        }
        return "noavatar.png";
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public Calendar getCreated() {
        return created;
    }

    public void setCreated(Calendar created) {
        this.created = created;
    }

    public String getEmailHash() {
        return emailHash;
    }

    public void setEmailHash(String emailHash) {
        this.emailHash = emailHash;
    }

    public int getHistoryLength() {
        return historyLength;
    }

    public void setHistoryLength(int historyLength) {
        this.historyLength = historyLength;
    }

    public boolean isNewsSubscription() {
        return newsSubscription;
    }

    public void setNewsSubscription(boolean newsSubscription) {
        this.newsSubscription = newsSubscription;
    }
    
    
}
